//
//  DFNewsVideoController.m
//  DFMessage
//
//  Created by lanou on 15/10/26.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "DFNewsVideoController.h"
#import "DFheadNewsView.h"
#import "Header.pch"
#import "PKRequestManager.h"
#import "NewsVideoModel.h"
#import "DFClickViedioViewController.h"
#import "MJRefresh.h"

@interface DFNewsVideoController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSMutableArray *DataSourceArray;
@property(nonatomic,strong)NewsVideoModel *model;

@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,assign)int page;
@end


@implementation DFNewsVideoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
 self.tableView  = [[UITableView alloc]initWithFrame:CGRectMake(375, 64, kScreenWidth, kScreenHeight - 64)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 83;
    
    [self.tableView registerClass:[DFheadNewsView class] forCellReuseIdentifier:@"ce"];
    
    [_scrollView addSubview:self.tableView];
    
    
    _DataSourceArray = [NSMutableArray array];

    [self requestDataWithType:0];
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestDataWithType:0];
    }];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self requestDataWithType:1];
    }];
}

-(void)requestDataWithType:(NSInteger)type
{

    if (type == 0) {
        _page = 1;
    }
    else
    {
        _page ++;
    }
    NSString *str = [NSString stringWithFormat:@"http://lib.wap.zol.com.cn/ipj/docList/?v=4.0&class_id=9&isReviewing=NO&last_time=2015-10-26%2010%3A15&page=%d&retina=1&vs=iph440",_page];
    [PKRequestManager requestWithType:GET urlString:str parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers |NSJSONReadingMutableLeaves error:nil];
        if (type == 0) {
            [self.DataSourceArray removeAllObjects];
        }
        NSArray *listArray = dic[@"list"];
      
        for (NSDictionary *modelDic in listArray) {
            _model = [[NewsVideoModel alloc]init];
            [_model setValuesForKeysWithDictionary:modelDic];
            [_DataSourceArray addObject:_model];
      
        }
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
     
    } error:^(NSError *error) {
        NSLog(@"%@",error);
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        
    }];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    NewsVideoModel *model = _DataSourceArray[indexPath.row];
 DFClickViedioViewController *ClickViedioVC = [[DFClickViedioViewController  alloc]init];
    ClickViedioVC.str = model.countid;
    [self.navigationController pushViewController:ClickViedioVC animated:NO];
    //[self presentViewController:ClickViedioVC animated:YES completion:nil];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _DataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DFheadNewsView *cell = [tableView dequeueReusableCellWithIdentifier:@"ce" forIndexPath:indexPath];
    cell.configureModel = _DataSourceArray[indexPath.row];
  
    return cell;
}




@end
